// pages/speedOfProgress/speedOfProgress.js
var session = require('../../common/session');
var config = require('../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showChoiceIndetity: true,
    result: null,
    shows:'',
    speedType:'Complete',
    totalCount:0,
    SkipCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '进度管理'
    });
    this.getSpeedOfProgressList(this.data.speedType);
  },
  onScanCode:function(){
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        let curCode = res.result;
        wx.redirectTo({
          url: '../progress/progress?codeNum='+curCode,
       });
      }
    })
  },
  // 任务列表
  getSpeedOfProgressList:function(status){
    wx.showLoading({
      title: '加载中...',
    });
    let _this = this;
    wx.request({
      url: config.service.GetAllProcessingOrderDto,
      data: {
        Sturt: status,
        IsSupplier: true,
        MaxResultCount: 10,
        SkipCount: _this.data.SkipCount
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': 'Bearer ' + session.get('xyznzzaccessToken')
      },
      success: function (res) {
        if(res.data && res.data.result && res.data.result.items && res.data.result.items instanceof Array){
          let result = res.data.result;
          let dataRes = _this.data.result;
          if(!dataRes) dataRes = [];
          _this.setData({
            result:  _this.data.SkipCount > 0 ? dataRes.concat(result.items):result.items,
            totalCount: result.totalCount
          });
        } else if(res.statusCode == '401'){
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
            duration: 2000,
            mask: true,
            success: function () {
              session.allClear();
              setTimeout(function(){
                wx.reLaunch({
                  url: 'pages/syslogin/syslogin'
                })
              },2000);
             
            }
          });
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete:function(){
        wx.hideLoading();
      }
    });
  },
  // 任务详情
  getSpeedOfProgressInfo:function(e){
    let data = e.target.dataset.data;
    wx.navigateTo({
      url: '../progress/progress?ProcessingOrderId='+data.id+'&orderProgressId='+data.orderProgressId,
   });
  },
  selectType:function(e){
    let type = e.target.dataset.type;
    if(type == this.data.speedType) return;
    this.setData({
      speedType: type,
      SkipCount: 0
    });
    this.getSpeedOfProgressList(type);
  },
  // 订单详情展示与否
  toggleChildBox:function(e){
    let id = e.currentTarget.dataset.id;
    let itemId = this.data.shows;
    this.setData({shows: id})
    if (id == itemId) {
      this.setData({
        shows: false
      })
    } else {
      this.setData({
        shows: id
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _SkipCount = this.data.SkipCount;
    if(_SkipCount < this.data.totalCount){
      _SkipCount += 10;
      this.setData({
        SkipCount:_SkipCount
      });
      this.getSpeedOfProgressList(this.data.speedType);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})